package forms;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.upload.FormFile;

public class DegreeForm extends ActionForm  {

	// ============== Debug Flag ================= //
	private static final boolean DEBUG = true;
	
	// =============== Form Variables ============= //
	private String location;
	private String university;
	private String major;
	private String month;
	private String year;
	private String gpa;
	private String title;
	private String filename;
	private String filesize;
	private FormFile transcript;
	
	// =============== Getter and Setters ================ //
	public String getLocation() {
		return location;
	}
	public void setLocation(String location) {
		this.location = location;
	}
	public String getUniversity() {
		return university;
	}
	public void setUniversity(String university) {
		this.university = university;
	}
	public String getMajor() {
		return major;
	}
	public void setMajor(String major) {
		this.major = major;
	}
	public String getMonth() {
		return month;
	}
	public void setMonth(String month) {
		this.month = month;
	}
	public String getYear() {
		return year;
	}
	public void setYear(String year) {
		this.year = year;
	}
	public String getGpa() {
		return gpa;
	}
	public void setGpa(String gpa) {
		this.gpa = gpa;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	
	public String getFilename() {
		return filename;
	}
	public void setFilename(String filename) {
		this.filename = filename;
	}
	public String getFilesize() {
		return filesize;
	}
	public void setFilesize(String filesize) {
		this.filesize = filesize;
	}
	public FormFile getTranscript() {
		return transcript;
	}
	public void setTranscript(FormFile transcript) {
		this.transcript = transcript;
	}
	
	/**
	 * Reset all properties to their default values.
	 * 
	 * @param mapping
	 *            The mapping used to select this instance
	 * @param request
	 *            The servlet request we are processing
	 */
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		this.setLocation(null);
		this.setUniversity(null);
		this.setMajor(null);
		this.setMonth(null);
		this.setYear(null);
		this.setGpa(null);
		this.setTitle(null);
		this.setTranscript(null);
	}
	
	/**
	 * Validate the input data. If validation fails, then report the errors in
	 * students.jsp, and specifically where <html:errors/> is located.
	 * 
	 * @param mapping
	 *            The mapping used to select this instance
	 * @param request
	 *            The servlet request we are processing
	 */
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {

		ActionErrors errors = new ActionErrors();
		String input = mapping.getInput();
		
		if (DEBUG) {
			System.err.println("StudentForm: "  + input);
		}
	
		if (input.equals("/students/discipline.jsp")) {
			
			if (gpa == null || gpa.length() < 1) {
				errors.add("GPAMsgTag1", new ActionMessage("errors.required",
				"GPA"));
			}
			else {
	
				try {
					Float.valueOf(gpa);
				}
				catch (NumberFormatException ex){
					errors.add("GPAMsgTag1", new ActionMessage("errors.notanumber",
					"GPA"));
				}
			}
		}
		
		return errors;
	}
}
